package com.zdb.demo.design.pattern.creational.singleton;

public class DoubleCheckSingleton {

    // volatile 禁止指令重排
    private volatile static DoubleCheckSingleton instance;

    private DoubleCheckSingleton() {
    }

    public static DoubleCheckSingleton getInstance() {
        if(instance == null) {
            synchronized (DoubleCheckSingleton.class) {
                if(instance == null) {
                    instance = new DoubleCheckSingleton();
                    // 存在指令2，3重排，导致对象没有初始化好时，其他线程使用报错
                    // 1. 分配内存
                    // 2. 初始化对象
                    // 3. 将instance指向内存地址
                }
            }
        }
        return instance;
    }
}
